package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysPlatformIdLink;
import com.settlement.system.mapper.SysPlatformIdLinkMapper;
import com.settlement.system.service.SysPlatformIdLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysPlatformIdLinkQuery;
import com.settlement.system.model.form.SysPlatformIdLinkForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysPlatformIdLinkImportVo;

/**
 * <p>
 * 各个平台的主子单号对应表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-09
 */
@Service
public class SysPlatformIdLinkServiceImpl extends ServiceImpl<SysPlatformIdLinkMapper, SysPlatformIdLink> implements SysPlatformIdLinkService {

    @Override
    public Page<SysPlatformIdLink> getPage(SysPlatformIdLinkQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysPlatformIdLink> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysPlatformIdLink(SysPlatformIdLinkForm sysPlatformIdLinkForm) {
         SysPlatformIdLink sysPlatformIdLink = new SysPlatformIdLink();
         BeanUtil.copyProperties(sysPlatformIdLinkForm, sysPlatformIdLink);
         return this.saveOrUpdate(sysPlatformIdLink);
     }

     @Override
     public SysPlatformIdLinkForm getSysPlatformIdLinkFormData(Long id) {
         SysPlatformIdLink sysPlatformIdLink = this.getById(id);
         SysPlatformIdLinkForm sysPlatformIdLinkForm = new SysPlatformIdLinkForm();
         BeanUtil.copyProperties(sysPlatformIdLink, sysPlatformIdLinkForm);
         return sysPlatformIdLinkForm;
     }

     @Override
     public boolean deleteSysPlatformIdLink(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysPlatformIdLinkImportVo> voList)  {
        List<SysPlatformIdLink> sysPlatformIdLinkList = new ArrayList<>();
        for (SysPlatformIdLinkImportVo importVo : voList) {
            SysPlatformIdLink sysPlatformIdLink = new SysPlatformIdLink();
            BeanUtil.copyProperties(importVo, sysPlatformIdLink);
            sysPlatformIdLinkList.add(sysPlatformIdLink);
        }
        this.saveOrUpdateBatch(sysPlatformIdLinkList);
    }

    @Override
    public List<SysPlatformIdLinkImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysPlatformIdLink> sysPlatformIdLinkList = this.getBaseMapper().selectBatchIds(ids);
        List<SysPlatformIdLinkImportVo> voList = new ArrayList<>();
        for (SysPlatformIdLink sysPlatformIdLink : sysPlatformIdLinkList) {
            SysPlatformIdLinkImportVo vo = new SysPlatformIdLinkImportVo();
            BeanUtil.copyProperties(sysPlatformIdLink, vo);
            voList.add(vo);
        }
        return voList;
    }
}
